package com.tedu._05sspringmvc2.controller;

import com.tedu._05sspringmvc2.mapper.UserMapper;
import com.tedu._05sspringmvc2.pojo.dto.UserDTO;
import com.tedu._05sspringmvc2.pojo.dto.UserUpdateDTO;
import com.tedu._05sspringmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tedu._05sspringmvc2.pojo.entity.User;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

//@Controller
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserMapper usermapper;
    @RequestMapping("/regUser")
    //@ResponseBody
    public String reg(UserDTO userdto){
        System.out.println("开始用户注册功能");
        System.out.println(userdto);
        User user = new User();
        BeanUtils.copyProperties(userdto,user);
        user.setCreated(new Date());
        int insert = usermapper.insert(user);
        return insert>0?"注册成功":"注册失败";
    }
    @RequestMapping("/deleteUser")
   // @ResponseBody
    public String delete(Integer id){
        System.out.println("开始处理用户删除...");
        int delete = usermapper.delete(id);

        return delete>0?"删除用户成功":"删除用户失败";
    }
    @RequestMapping("/updateUser")
    //@ResponseBody
    public String updataUser(UserUpdateDTO updatedto){
        System.out.println("开始处理用户修改操作");
        System.out.println(updatedto);
        User user = new User();
        BeanUtils.copyProperties(updatedto,user);
        int update = usermapper.update(user);
        return update>0?"修改成功":"修改失败";
    }
    @RequestMapping("/findAll")
    //@ResponseBody
    public List<UserVO> findAll(){
        System.out.println("查询所有用户信息");
        List<UserVO> all = usermapper.findAll();
        System.out.println(all);
        return all;
    }
}
